﻿var formatter = new (function () {

    this.culture = "pt-br";

    this.daysOfWeekMin = {
        'en-us': ['S', 'M', 'T', 'W', 'Th', 'F', 'Sa', 'S'],
        'pt-br': ['D', 'S', 'T', 'Q', 'Q', 'S', 'S', 'D'],
        'es-ar': ['D', 'L', 'M', 'Mi', 'J', 'V', 'S', 'D']
    };

    this.daysOfWeekShort = {
        'en-us': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        'pt-br': ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb', 'Dom'],
        'es-ar': ['Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb', 'Dom']
    };

    this.monthNamesShort = {
        'en-us': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
        'pt-br': ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
        'es-ar': ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic']
    };

    this.daysOfWeek = {
        'en-us': ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", 'Saturday', "Sunday"],
        'pt-br': ["Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sábado", "Domingo"],
        'es-ar': ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado", "Domingo"]
    };

    this.dayOfWeekShort = function (d) {
        return formatter.daysOfWeekShort[formatter.culture][d];
    }

    this.dayOfWeek = function (d) {
        return formatter.daysOfWeek[formatter.culture][d];
    };

    this.monthNames = {
        'en-us': ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
        'pt-br': ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
        'es-ar': ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio​​', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre']
    };

    this.monthName = function (m) {
        return formatter.monthNames[formatter.culture][m - 1];
    };

    var toTwoDigits = function (number) {
        var parsed = parseInt(number, 10);
        return (parsed < 10 ? "0" : "") + parsed;
    };

    var dateValuesShortFunctions = {
        "pt-br": function (dt) { return toTwoDigits(dt.Day) + "/" + toTwoDigits(dt.Month) + "/" + dt.Year; },
        "en-us": function (dt) { return toTwoDigits(dt.Month) + "/" + toTwoDigits(dt.Day) + "/" + dt.Year; },
        "es-ar": function (dt) { return toTwoDigits(dt.Day) + "/" + toTwoDigits(dt.Month) + "/" + dt.Year; }
    };

    /* returns specific day/month/year type */
    this.dateValuesShort = function (dt) {
        return dateValuesShortFunctions[formatter.culture](dt);
    };

    var dateValuesLongFunctions = {
        "pt-br": function (dt) { return formatter.dayOfWeek(dt.DayOfWeek) + ", " + dt.Day + " de " + formatter.monthName(dt.Month) + " de " + dt.Year; },
        "en-us": function (dt) { return formatter.dayOfWeek(dt.DayOfWeek) + ", " + formatter.monthName(dt.Month) + " " + dt.Day + ", " + dt.Year; },
        "es-ar": function (dt) { return formatter.dayOfWeek(dt.DayOfWeek) + ", " + dt.Day + " de " + formatter.monthName(dt.Month) + " de " + dt.Year; }
    };

    this.dateValuesLong = function (dt) {
        return dateValuesLongFunctions[formatter.culture](dt);
    };

    var dateValuesLongAbbreviatedFunctions = {
        "pt-br": function (dt) { return formatter.dayOfWeekShort(dt.DayOfWeek) + ", " + dt.Day + " " + formatter.monthName(dt.Month) + " " + dt.Year; },
        "en-us": function (dt) { return formatter.dayOfWeekShort(dt.DayOfWeek) + ", " + formatter.monthName(dt.Month) + " " + dt.Day + ", " + dt.Year; },
        "es-ar": function (dt) { return formatter.dayOfWeekShort(dt.DayOfWeek) + ", " + dt.Day + " " + formatter.monthName(dt.Month) + " " + dt.Year; }
    };

    this.dateValuesLongAbbreviated = function (dt) {
        return dateValuesLongAbbreviatedFunctions[formatter.culture](dt);
    };

    this.dateValuesToDate = function (dt) {
        if (dt.Year != undefined && dt.Month != undefined && dt.Day != undefined)
            return new Date(dt.Year, dt.Month - 1, dt.Day);

        return null;
    };

    /* return 13:00 mode */
    this.timeValuesShort = function (dt) {
        return (dt.Hour < 10 ? "0" : "") + dt.Hour + ":" + (dt.Minutes < 10 ? "0" : "") + dt.Minutes;
    };

    var timeValuesLongFunctions = {
        "pt-br": function (dt) { return (dt.Hour < 10 ? "0" : "") + dt.Hour + (dt.Hour > 1 ? " horas e " : " hora e ") + (dt.Minutes < 10 ? "0" : "") + dt.Minutes + (dt.Minutes > 1 ? " minutos" : " minuto"); },
        "en-us": function (dt) { return (dt.Hour < 10 ? "0" : "") + dt.Hour + (dt.Hour > 1 ? " hours and " : " hour and ") + (dt.Minutes < 10 ? "0" : "") + dt.Minutes + (dt.Minutes > 1 ? " minutes" : " minute"); },
        "es-ar": function (dt) { return (dt.Hour < 10 ? "0" : "") + dt.Hour + (dt.Hour > 1 ? " horas y " : " hora y ") + (dt.Minutes < 10 ? "0" : "") + dt.Minutes + (dt.Minutes > 1 ? " minutos" : " minuto"); }
    };

    this.dateMonthYearLong = function (dt) {
        return formatter.dayOfWeekShort[dt.DayOfWeek] + ", " + dt.Day + " " + formatter.monthName[dt.Month] + " " + dt.Year;
    };

    this.timeValuesLong = function (dt) {

        if (!isNaN(dt)) {
            var rest = dt % 60;
            var hours = (dt - rest) / 60;
            dt = { Hour: hours, Minutes: rest };
        }

        return timeValuesLongFunctions[formatter.culture](dt);
    };

    this.dateTimeValuesComplete = function (dt) {
        return dateValuesShortFunctions[formatter.culture](dt) + " " + formatter.timeValuesShort(dt);
    };

    this.currencyDefaults = {
        "BRL": { thousandSeparator: ".", decimalSeparator: ",", symbol: "R$" },
        "USD": { thousandSeparator: ".", decimalSeparator: ",", symbol: "US$" },
        "ARS": { thousandSeparator: ".", decimalSeparator: ",", symbol: "$" }
    };

    this.currencyType = function () {
        switch (formatter.culture) {
            case "pt-br":
                return "BRL"; break;
            case "en-us":
                return "USD"; break;
            case "es-ar":
                return "ARS"; break;
            default:
                return "BRL";
        }
    };

    this.currency = function (number, useCurrencySymbol, precision, currency) {

        try {
            if (isNaN(precision) || Math.abs(precision) > 10)
                precision = 0;
        } catch (e) {
            precision = 0;
        }

        try {
            number = parseFloat(number + '');
            if (isNaN(number))
                return "NaN";
        } catch (e) {
            return "NaN";
        }

        number = Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision);
        var integerpart = '' + ((number < 0) ? Math.ceil(number) : Math.floor(number));
        var decimalpart = Math.abs(Math.round((number - integerpart) * (Math.pow(10, precision))));

        if (decimalpart < 10)
            decimalpart = "0" + decimalpart;

        if (decimalpart == 0)
            decimalpart = "00";

        var buff = "";

        if(!currency)
            currency = formatter.currencyType()

        for (var j = -1, i = integerpart.length; i >= 0; i--, j++) {

            if ((j % 3) == 0 && j > 1)
                buff = formatter.currencyDefaults[currency].thousandSeparator + buff;

            buff = integerpart.charAt(i) + buff;
        }

        if (precision > 0)
            buff += formatter.currencyDefaults[currency].decimalSeparator + decimalpart;

        if (useCurrencySymbol == undefined || useCurrencySymbol)
            buff = formatter.currencyDefaults[currency].symbol + " " + buff;

        return buff;
    };

    /* DISTANCES */
    var kilometersToMiles = function (value) {
        return value / 1.609;
    };

    var formattedDistances = {
        "pt-br": function (distance) { return distance + " km"; },
        "en-us": function (distance) { return kilometersToMiles(distance) + " mi"; },
        "es-ar": function (distance) { return distance + " km"; }
    };

    this.distance = function (value) {
        value = parseFloat(parseFloat(value).toFixed(2));
        return formattedDistances[formatter.culture](value);
    };

    /* TIMES */
    this.numericTime = function (dt) {
        return parseFloat(dt.Hour + (dt.Minutes < 10 ? "0" : "") + dt.Minutes);
    };

    var zipCodeMasks = {
        "pt-br": "99999-999",
        "en-us": "",
        "es-ar": "a9999aaa"
    };

    this.zipCodeMask = function () {
        return zipCodeMasks[formatter.culture];
    };

    var phoneNumberMasks = {
        "pt-br": "9999-9999",
        "en-us": "",
        "es-ar": ""
    };

    this.phoneNumberMask = function () {
        return phoneNumberMasks[formatter.culture];
    };

    var phonePrefixMasks = {
        "pt-br": "(99)",
        "en-us": "",
        "es-ar": ""
    };

    this.phonePrefixMask = function () {
        return phonePrefixMasks[formatter.culture];
    };

    var documentMasks = {
        "pt-br": "999.999.999-99",
        "en-us": "",
        "es-ar": "99999999-a"
    };

    this.documentMask = function () {
        return documentMasks[formatter.culture];
    }

    var zipCodeValidations = {
        "pt-br": function (cep) { return cep.length == 9; },
        "en-us": function (cep) { return true; },
        "es-ar": function (cep) { return true; }
    };

    this.zipCodeValidation = function (cep) {
        return zipCodeValidations[formatter.culture](cep);
    };

    var documentValidations = {
        "pt-br": function (document) { return document.isCPF(); },
        "en-us": function (document) { return true; },
        "es-ar": function (document) { return true; }
    };

    this.documentValidation = function (document) {
        return documentValidations[formatter.culture](document);
    };

    var phoneNumberValidations = {
        "pt-br": function (phoneNumber) { return phoneNumber.length == 8; },
        "en-us": function (phoneNumber) { return true; },
        "es-ar": function (phoneNumber) { return true; }
    };

    this.phoneNumberValidation = function (phoneNumber) {
        return phoneNumberValidations[formatter.culture](phoneNumber);
    };

    var prefixNumberValidations = {
        "pt-br": function (prefixNumber) { return prefixNumber.length == 2; },
        "en-us": function (prefixNumber) { return true; },
        "es-ar": function (prefixNumber) { return true; }
    };

    this.prefixNumberValidation = function (prefixNumber) {
        return prefixNumberValidations[formatter.culture](prefixNumber);
    };

    var listOfStates = {
        "pt-br": ["AC", "AL", "AP", "AM", "BA", "CE", "DF", "ES", "GO", "MA", "MT", "MS", "MG", "PA", "PB", "PR", "PE", "PI", "RJ", "RN", "RS", "RO", "RR", "SC", "SP", "SE", "TO"],
        "en-us": [],
        "es-ar": ["Buenos Aires", "Catamarca", "Chaco", "Chubut", "Ciudad Autonoma De Bs.As.", "Cordoba", "Corrientes", "Entre Rios", "Formosa", "Jujuy", "La Pampa", "La Rioja", "Mendoza", "Misiones", "Neuquen", "Rio Negro", "Salta", "San Juan", "San Luis", "Santa Cruz", "Santa Fe", "Stago. Del Estero", "Tierra Del Fuego", "Tucuman"]
    };

    this.states = function () {
        return listOfStates[formatter.culture];
    };


})();


